//
//  BTEHomeViewController.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTEProductTwoViewController: BTEBaseViewController {
    
    var time: Date?
    
    let server = BTEProductToolNetServer()
    
    var model: BTEProductpeeebonbloidNetModel?
    
    var listmodel: BTECarnepalaeoglaciologyNetModel? {
        didSet {
            guard let m = listmodel else {
                return
            }
            v.model = m.brokers
        }
    }
    
    var pid: String?
    
    var v: BTEProductTwoView {
        return view as! BTEProductTwoView
    }
    
    override func loadView() {
        view = BTEProductTwoView()
    }

    init(model: BTEProductpeeebonbloidNetModel) {
        self.model = model
        super.init(nibName: nil, bundle: nil)
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        time = Date()
        
        titleLabel.text = model?.aftermath ?? ""
        
        v.list.snp.remakeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navigationBar.snp.bottom)
            make.bottom.equalTo(v.button.snp.top)
        }
        
        v.list.isHidden = true
        v.button.isHidden = true
        
        load()
        
        v.button.addTarget(self, action: #selector(push), for: .touchUpInside)
    }

}



extension BTEProductTwoViewController {
    
    @objc func load(){
        BTEHUD.shared.showLoader(on: view)
        server.palaeoglaciology(data: ["privileger":pid!]) {[weak self] result in
            switch result {
            case .success(let success):
                BTEHUD.shared.hide()
                self?.v.list.isHidden = false
                self?.v.button.isHidden = false
                
                success?.sidelingwise?.brokers?.forEach { item in
                    item.panderism?.forEach({$0.choised = false})
                    item.panderism?.filter({$0.dikegrave == item.dikegrave}).first?.choised = true
                }

                self?.listmodel = success?.sidelingwise
                break
            case .failure(let failure):
                switch failure {
                case .Error(c: let b, m: let bd):
                    BTEHUD.shared.showError(bd, on: (self?.view)!)
                    break
                default:
                    break
                }
                break
            }
        }
    }
    
    @objc func push(){
        view.endEditing(true)
        BTEHUD.shared.showLoader(on: view)
        
        var result: [String: String] = [:]

        for item in (listmodel?.brokers ?? []) {
            if let match = item.panderism?.first(where: { $0.manificum == item.tripylaean }) {
                result[item.geothermally!] = match.dikegrave
            } else {
                result[item.geothermally!] = item.tripylaean
            }
        }
        
        result["privileger"] = pid
        
        server.vasovagal(data:result) {[weak self] result in
            switch result {
            case .success(let success):
                let t = TrackingEvent.init(step: "4", startTimestamp: "\((self?.time)!.timeIntervalSince1970)", endTimestamp: "\(Date().timeIntervalSince1970)")
                BTELocationReporterManager.shared.reportLocation(for: t)
                BTEHUD.shared.hide()
                self?.navigationController?.popViewController(animated: true)
                break
            case .failure(let failure):
                switch failure {
                case .Error(c: let b, m: let bd):
                    BTEHUD.shared.showError(bd, on: (self?.view)!)
                    break
                default:
                    break
                }
                break
            }
        }
    }
    
}
